<?php

namespace backend\modules\logs\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\modules\logs\models\CreditOrderLog;

/**
 * CreditOrderLogSearch represents the model behind the search form about `backend\modules\logs\models\CreditOrderLog`.
 */
class CreditOrderLogSearch extends CreditOrderLog
{
    public $post_idno;
    public $credit_idno;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'credit_order_id', 'time', 'old_status', 'new_status'], 'integer'],
            [['post_idno', 'credit_idno'], 'string'],
            [['post_data', 'send_return_msg', 'credit_order_data', 'api_return_code', 'api_return_msg', 'serial_number'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = CreditOrderLog::find()->orderBy(['id'=>SORT_DESC]);

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'credit_order_id' => $this->credit_order_id,
            'time' => $this->time,
            'old_status' => $this->old_status,
            'new_status' => $this->new_status,
        ]);

        $query->andFilterWhere(['like', 'post_data', $this->post_data])
            ->andFilterWhere(['like', 'send_return_msg', $this->send_return_msg])
            ->andFilterWhere(['like', 'credit_order_data', $this->credit_order_data])
            ->andFilterWhere(['like', 'api_return_code', $this->api_return_code])
            ->andFilterWhere(['like', 'api_return_msg', $this->api_return_msg])
            ->andFilterWhere(['like', 'serial_number', $this->serial_number]);
        $query = $query->andFilterWhere(['like', 'post_data', $this->post_idno]);
        $query = $query->andFilterWhere(['like', 'credit_order_data', $this->credit_idno]);

        return $dataProvider;
    }
}
